package com.weirong.smartvault.backservice.util.chart;

import com.weirong.smartvault.common.tools.spring.context.SpringContextUtil;
import com.weirong.smartvault.backservice.util.statistics.Statistics;
import com.weirong.smartvault.backservice.util.statistics.StatisticsType;
import com.weirong.smartvault.dao.ForeignCurrencyTypeRepository;
import com.weirong.smartvault.entity.ForeignCurrencyType;

import java.lang.reflect.InvocationTargetException;

/**
 * Created with IntelliJ IDEA.
 * User: kfeb4
 * Date: 2017/12/25
 * Time: 10:42
 */
public class ChartUse {
    static ForeignCurrencyTypeRepository foreignCurrencyTypeRepository = SpringContextUtil.getBean( ForeignCurrencyTypeRepository.class);
    public static ChartOption buildOption(String date) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
        ChartOption option = new ChartOption();
        Integer year = Integer.valueOf(date);
        for (ForeignCurrencyType foreignCurrencyType : foreignCurrencyTypeRepository.findAll()) {
            option.legend.data.add(foreignCurrencyType.getName());
        }

        option.title.text = date + "年使用统计表";

        for (String type : option.legend.data) {
            ChartOption.Series series = new ChartOption.Series();
            option.series.add(series);

            series.name = type;
            series.type = "bar";
            ChartOption.Series.MakePoint.Type maxType = new ChartOption.Series.MakePoint.Type();
            series.makePoint.data.add(maxType);
            maxType.name = "最大值";
            maxType.type = "max";
            ChartOption.Series.MakePoint.Type minType = new ChartOption.Series.MakePoint.Type();
            series.makePoint.data.add(minType);
            minType.name = "最小值";
            minType.type = "min";
            ChartOption.Series.MakeLine.Type averageType = new ChartOption.Series.MakeLine.Type();
            series.makeLine.data.add(averageType);
            averageType.name = "平均值";
            averageType.type = "average";
            String typeCode = foreignCurrencyTypeRepository.getByName(type).getCode();

            //按月获取数据，并填充空白和计算总和后填入data
            for (int i = 0; i < 12; i++) {
                series.data.add((float) Statistics.getValue(StatisticsType.USEMONTH, typeCode, year, i));
            }
        }

        //重新整理
        return option;
    }
}
